﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using common;


public partial class Form_sql : Form
{
    private static Form_sql fs;
    public string sql;

    private Form_sql()
    {
        InitializeComponent();


    }

    public static Form_sql get_instance()
    {
        if (fs == null || fs.IsDisposed) { fs = new Form_sql(); }
        return fs;
    }

    private void Form_sql_Load(object sender, EventArgs e)
    {

        
        //du
        XmlDocument doc = new XmlDocument();
        string strPath = "Xml/sql.xml";
        doc.Load(strPath);
        XmlNodeList nodeList = doc.SelectSingleNode("data").ChildNodes;

        string type = "";
        string use = "";
        string server = "";
        string database = "";
        string uid = "";
        string pwd = "";


        foreach (XmlNode xn in nodeList)
        {
            if (xn.Name == "row")
            {
                foreach (XmlNode xn1 in xn.ChildNodes)
                {


                    if (xn1.Name == "type")
                    {

                        type = xn1.InnerText.Trim();

                    }
                    else if (xn1.Name == "use")
                    {

                        use = xn1.InnerText.Trim();
                    }

                    else if (xn1.Name == "server")
                    {
                        server = xn1.InnerText.Trim();

                    }
                    else if (xn1.Name == "database")
                    {

                        database = xn1.InnerText.Trim();
                    }

                    else if (xn1.Name == "uid")
                    {
                        uid = xn1.InnerText.Trim();

                    }
                    else if (xn1.Name == "pwd")
                    {
                        pwd = xn1.InnerText.Trim();

                    }



                }

                if (server == "") {
                    MessageBox.Show("xml配置有错");
                    return;
                }

                //cao zuo
                if (use == "1")
                {
                    db_helper db = db_factory.get_db(type);
                    db_helper.database = database;
                    db_helper.server = server;
                    db_helper.pwd = pwd;
                    db_helper.uid = uid;
                    //string strCon = "server=" + datasource + ";user id=" + uid + "; pooling=false;charset=gb2312; password=" + pwd + "; Allow Zero Datetime=True";
                    //string strCon = "server=" + datasource + ";database=;uid=" + uid + ";pwd=" + pwd + ";";
                    try
                    {
                        this.dataGridView1.DataSource = sqlserver_helper.get_data_table(sql);
                        this.richTextBox1.Text = sqlserver_helper.ExecuteNonQuery(sql).ToString();
                    }
                    catch (Exception ee)
                    {
                        this.richTextBox1.Text += "\n\n" + ee.Message;
                    }
                    finally {
                        this.Focus();
                    }


                    
                    break;
                }


                


            }

        }
    }

    private void Form_sql_KeyPress(object sender, KeyPressEventArgs e)
    {
        
    }

    private void Form_sql_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Escape)
        {

            this.Close();
            e.SuppressKeyPress = true; // don't pass the event down
        }
    }
}
